﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using SD.Database.Classes;
using System.Collections.Generic;
using DBAccess;

namespace SD.Database
{
    public partial class StoredProcedures
    {
        public static void CreateUser(string name, string email, string password, int attack, int defnese, int speed, int culture, int production, int science, int growth, bool startupBonus)
        {
            //get avg stats
            Dictionary<string, double> stats = Stats.AvgSciences();

            //if (attack + defnese + speed + culture + production + science + growth != 0) return;
            int pId = 0;
            int oId = int.Parse(Configuration.GetConfigurationForOption("LastOrginID"));
            int playersInSystem = int.Parse(Configuration.GetConfigurationForOption("PlayersInLastSystem"));

            do
            {
                if (playersInSystem > 2)
                {
                    oId = oId  + 1;
                    playersInSystem = 0;
                }
               

                pId = 2 + 4 * playersInSystem;
                playersInSystem++;
            } while (Planets.IsEmptyPlanet(oId, pId) != true);


            InsertNews(8, DateTime.Now, "Create player: lo: " + oId.ToString() + " pis:" + playersInSystem.ToString() + " oid: " + oId.ToString() + " pid: " + pId.ToString());
            //store configuration
            DAL.GetInstance().UpdateData("Update configuration set LastOrginID=@param0, PlayersInLastSystem=@param1", new List<object>() { oId, playersInSystem });

            if (DAL.GetInstance().GetInt("SELECT Count(*) FROM Players WHERE (PlayerName  = @param0 )", new List<object>() { name }) > 0)
                return;

            double cultureVal;
            if (stats["Culture"] > 1)
                cultureVal = stats["Culture"] * 0.85;
            else
                cultureVal = 1;
            double social;
            if (startupBonus == true)
                social = stats["Social"] * 0.85 + 2.0;
            else
                social = stats["Social"] * 0.85;

            DAL.GetInstance().InsertData(@"Insert into Players(PlayerName,email, Password,Attack,Defense,Speed,Culture,Production,Growth,Science, OrginID, CurrentCulture, CurrentScience, ExpPoints, ArtifactID, Biology, Economy, Energy, Math, Physics, Social ) 
                    Values (@param0,@param1, @param2,@param3,@param4,@param5,@param6,@param7,@param8,@param9, @param10, @param11, 1, 0, 0, @param12, @param13, @param14, @param15, @param16, @param17)",
                new List<object>() { name, email, StaticMethods.GetMd5Sum(name + password) , attack, defnese,speed , culture, production, growth, science, oId, 
                 cultureVal , stats["Biology"] * 0.85, stats["Economy"] * 0.85, stats["Energy"] * 0.85, stats["Physics"] * 0.85,
                 stats["Math"] * 0.85, social });

            Planets.ColonizePlanet(oId, pId, UserFunctions.GetPlayerID(name));
            if (startupBonus == true)
            {
                DAL.GetInstance().UpdateData("Update planets Set Labs = 10 Where PlanetId= @param0 and SystemId = @param1", new List<object>() { pId, oId });
            }
            TimeSpan ts = DateTime.Now - Configuration.GetGameStart();

            Planets.AddProductionPoints(oId, pId, 10000 + (int)(ts.TotalHours * Stats.GetAvgProduction() * 0.85));
        }
    };
}